﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;

using Marking.Dashboard.Infrastructures;
using Marking.Dashboard.ViewModels.Sales;
using Marking.Dashboard.ViewModels.Finance;

namespace Marking.Dashboard.Services.Locals
{
    public class LocalSalesService : ISalesService
    {
        private static List<string> _mockItemNames = new List<string>
        {
            "汇款",
            "现金",
            "支票",
            "转账",
            "预付款"
        };

        public void LoadClothSalesInfos(
            string cycleType,
            DateTime startDate,
            DateTime endDate,
            Action<IEnumerable<SalesInfo>> action)
        {
            this.MockSalesResult(cycleType, startDate, endDate, action);
        }

        public void LoadMoneySalesInfos(
            string cycleType,
            DateTime startDate,
            DateTime endDate,
            Action<IEnumerable<SalesInfo>> action)
        {
            this.MockSalesResult(cycleType, startDate, endDate, action);
        }

        private void MockSalesResult(
            string cycleType,
            DateTime startDate,
            DateTime endDate,
            Action<IEnumerable<SalesInfo>> action)
        {
            var results = new List<SalesInfo>();

            foreach (var item in CycleTypeHelper.SimulateDates(cycleType, startDate, endDate))
            {
                results.Add(new SalesInfo
                {
                    StartDate = item,
                    EndDate = item,
                    Quantity = new Random(Guid.NewGuid().GetHashCode()).Next(100, 500),
                    CycleType = cycleType,
                    ExtraInfos = new FinanceWindowModel
                    {
                        ReceivableInfos = this.MockFinanceResult("收款明细，合计（{0:N0}）元。"),
                        PayableInfos = this.MockFinanceResult("退款明细，合计（{0:N0}）元。"),
                    }
                });
            }

            action(results);
        }

        private FinancePieChartViewModel MockFinanceResult(string title)
        {
            var result = new FinancePieChartViewModel(title);

            _mockItemNames.ForEach(x =>
            {
                result.Add(new FinanceInfo
                {
                    ItemName = x,
                    Quantity = new Random(Guid.NewGuid().GetHashCode()).Next(100, 500)
                });
            });

            return result;
        }
    }
}
